package org.dentalcard.ui.schedule.tools;

import org.dentalcard.entity.Shift;
import org.dentalcard.service.impl.ScheduleDaoImpl;
import org.dentalcard.ui.api.tools.AbstractDecTableModel;
import org.dentalcard.ui.main.MainFrameBuilder;

import java.util.List;

/**
 * @user: Vladimir Chernishov
 * @date: 21.12.11
 * @time: 10:25
 */
public class ShiftTableModel extends AbstractDecTableModel {

    private ScheduleDaoImpl shiftDao;
    private List<Shift> shiftList;
    
    public ShiftTableModel() {
        super(new String[]{"#", "name", "from", "till"});
        shiftDao = new ScheduleDaoImpl();
        reload();
    }

    public void reload() {
        shiftList = shiftDao.getShiftList(MainFrameBuilder.getInstance().getDecUser());
        fireTableDataChanged();
    }

    @Override
    public int getRowCount() {
        if(shiftList.isEmpty()){
            return 1;
        }
        return shiftList.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if(shiftList.isEmpty()){
            return "";
        }
        Shift shift = shiftList.get(rowIndex);
        switch (columnIndex) {
            case 0 :{
                return shift.getnByOrder();
            }
            case 1 :{
                return shift.getName();
            }
            case 2 :{
                return shift.getFromTime();
            }
            case 3 :{
                return shift.getTillTime();
            }
        } 
        return "";
    }

    public Shift getShift(int selectedRow) {
        if(shiftList.isEmpty()){
            return null;
        }
        return shiftList.get(selectedRow);
    }
}
